package com.chalk.config.listener;


import com.chalk.common.constant.CommonConstants;
import com.chalk.common.constant.TypeConstants;
import com.chalk.common.util.RedisUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.Calendar;

/**
 * Redis Key 过期监听类
 *
 * @author Zhaob
 */
//@Component
//public class CustomKeyExpiredListener extends KeyExpirationEventMessageListener {
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(CustomKeyExpiredListener.class);
//
////    @Autowired
////    private ActivityCenterService activityCenterService;
//    @Autowired
//    private RedisUtil redisUtil;
//
//    public CustomKeyExpiredListener(RedisMessageListenerContainer listenerContainer){
//        super(listenerContainer);
//    }
//
//    @Override
//    public void onMessage(Message message, byte[] pattern) {
//        // 获取过期的key
//        String expKey = message.toString();
//        LOGGER.info("-------------监听到过期Key--------------->"+expKey);
//        //如果为活动中心key
//        if(expKey.contains(CommonConstants.ACTIVITY_CENTER_NOT_STARTED)){
//            activityCenterStart(expKey.split(":")[1]);
//        }else if(expKey.contains(CommonConstants.ACTIVITY_CENTER_PROCESSING)){
//            activityCenterEnd(expKey.split(":")[1]);
//        }
//    }
//
//
////    /**
////     * 更新活动中心活动状态进行中
////     *
////     * @param id 数据主键
////     */
////    private void activityCenterStart(String id) {
////        ActivityCenter center = activityCenterService.selectById(id);
////        if(center != null){
////            String key = CommonConstants.ACTIVITY_CENTER_PROCESSING + center.getId();
////            long time = (center.getEndTime().getTime() - Calendar.getInstance().getTimeInMillis())/1000;
////            if(time > 0){
////                center.setStatus(TypeConstants.ActivityStatus.ING.getValue());
////                redisUtil.set(key,center.getId(),time);
////            }else{
////                center.setStatus(TypeConstants.ActivityStatus.END.getValue());
////            }
////            activityCenterService.updateById(center);
////        }
////    }
////
////    /**
////     * 更新活动中心活动状态为结束
////     *
////     * @param id 数据主键
////     */
////    private void activityCenterEnd(String id) {
////        ActivityCenter center = activityCenterService.selectById(id);
////        if(center != null){
////            center.setStatus(TypeConstants.ActivityStatus.END.getValue());
////            activityCenterService.updateById(center);
////        }
////    }
//
//}
